package com.chlitina.store.modules.cash.model;

import java.util.ArrayList;
import java.util.List;

import com.chlitina.store.common.utils.Utils;
import com.chlitina.store.modules.base.entity.StoreCardTemplateAll;
import com.chlitina.store.modules.base.entity.StoreCardTemplateDuration;
import com.chlitina.store.modules.base.entity.StoreCardTemplateProduct;
import com.chlitina.store.modules.base.entity.StoreCardTemplateTimes;
import com.chlitina.store.modules.employee.entity.Employee;

public class NewCards {
	private int uuid = 0;
	private List<CardCart> allCards = new ArrayList<CardCart>();
	private List<CardCart> productCards = new ArrayList<CardCart>();
	private List<CardCart> durationCards = new ArrayList<CardCart>();
	private List<CardCart> timesCards = new ArrayList<CardCart>();
	private CardTemplates templates = null;
	private NewCardsPayment cardsPayment = new NewCardsPayment(this);
	private int count;
	private Employee employee;

	public void updateCardCount() {
		details.reset();
		List<NewCardItem> items = details.getItems();
		for (CardCart cart : allCards) {
			items.add(NewCardItem.create(cart));
		}
		for (CardCart cart : productCards) {
			items.add(NewCardItem.create(cart));
		}
		for (CardCart cart : timesCards) {
			items.add(NewCardItem.create(cart));
		}
		for (CardCart cart : durationCards) {
			items.add(NewCardItem.create(cart));
		}
		for (int index = 0; index < items.size(); index++) {
			items.get(index).setIndex(index);
		}
		count = items.size();

		List<String> payments = details.getPayments();
		for (AllCardPaymentDetail detail : cardsPayment
				.getCardsPaymentDetails()) {
			payments.add("耗卡：【"
					+ detail.getCardPayment().getCard().getTemplate().getName()
					+ "】, " + detail.getCardPayment().getMoney() + " 元");
		}
		if (cardsPayment.getTotalCashCost() > 0) {
			payments.add("现金：付现 " + cardsPayment.getTotalCashCost() + " 元");
		}
	}

	private NewCardsDetails details = new NewCardsDetails();

	public List<CardCart> getAllCards() {
		return allCards;
	}

	public List<CardCart> getProductCards() {
		return productCards;
	}

	public List<CardCart> getDurationCards() {
		return durationCards;
	}

	public List<CardCart> getTimesCards() {
		return timesCards;
	}

	public NewCards(CardTemplates cardTemplates) {
		setTemplates(cardTemplates);
	}

	public CardTemplates getTemplates() {
		return templates;
	}

	/**
	 * 计算新开卡的实际需支付金额 当用户新增开卡，或者用户取消待开的开，该方法需被调用
	 */
	public void updateRealTotalToPay() {
		// 按卡类型分别计算零售价格，以及实际价格汇总

	}

	public void setTemplates(CardTemplates templates) {
		this.templates = templates;
	}

	public StoreCardTemplateTimes getStoreCardTemplateTimesById(long templateID) {
		if (templates == null) {
			return null;
		}
		List<StoreCardTemplateTimes> timesCardTemplates = templates
				.getTimesCardTemplates();
		for (StoreCardTemplateTimes template : timesCardTemplates) {
			if (Utils.equal(template.getId(), templateID)) {
				return template;
			}
		}
		return null;
	}

	
	public StoreCardTemplateProduct getStoreCardTemplateProductById(
			long templateID) {
		if (templates == null) {
			return null;
		}
		List<StoreCardTemplateProduct> productCardTemplates = templates
				.getProductCardTemplates();
		for (StoreCardTemplateProduct template : productCardTemplates) {
			if (Utils.equal(template.getId(), templateID)) {
				return template;
			}
		}
		return null;
	}

	public StoreCardTemplateAll getStoreCardTemplateAllById(long templateID) {
		if (templates == null) {
			return null;
		}
		List<StoreCardTemplateAll> allCardTemplates = templates
				.getAllCardTemplates();
		for (StoreCardTemplateAll template : allCardTemplates) {
			if (Utils.equal(template.getId(), templateID)) {
				return template;
			}
		}
		return null;
	}

	public StoreCardTemplateDuration getStoreCardTemplateDurationById(
			long templateID) {
		if (templates == null) {
			return null;
		}
		List<StoreCardTemplateDuration> durationCardTemplates = templates
				.getDurationCardTemplates();
		for (StoreCardTemplateDuration template : durationCardTemplates) {
			if (Utils.equal(template.getId(), templateID)) {
				return template;
			}
		}
		return null;
	}

	public boolean isEmpty() {
		if (!getAllCards().isEmpty()) {
			return false;
		} else if (!getDurationCards().isEmpty()) {
			return false;
		} else if (!getTimesCards().isEmpty()) {
			return false;
		} else if (!getProductCards().isEmpty()) {
			return false;
		}
		return true;
	}

	public NewCardsPayment getCardsPayment() {
		return cardsPayment;
	}

	public void setCardsPayment(NewCardsPayment cardsPayment) {
		this.cardsPayment = cardsPayment;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public NewCardsDetails getDetails() {
		return details;
	}

	public void setDetails(NewCardsDetails details) {
		this.details = details;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public int getUuid() {
		return uuid;
	}

	public int generateUUID() {
		return ++uuid;
	}
}
